package com.lsp.lib_toolkit;

import android.graphics.drawable.AnimationDrawable;
import android.view.View;

/**
 * UI相关工具类
 *
 * @author lsp
 * @created 16/8/16 11:01
 */
public class ViewUtils {

  public static int startViewAnimation(View view) {
    final AnimationDrawable animationDrawable = (AnimationDrawable) view.getBackground();
    view.post(new Runnable() {
      @Override public void run() {
        animationDrawable.start();
      }
    });
    return getTotalDuration(animationDrawable);
  }

  private static int getTotalDuration(AnimationDrawable animationDrawable) {
    if (animationDrawable == null || animationDrawable.getNumberOfFrames() <= 0) {
      return 0;
    }
    int iDuration = 0;
    for (int i = 0; i < animationDrawable.getNumberOfFrames(); i++) {
      iDuration += animationDrawable.getDuration(i);
    }
    return iDuration;
  }

  public static void stopViewAnimation(View view) {
    AnimationDrawable animationDrawable = (AnimationDrawable) view.getBackground();
    animationDrawable.stop();
    animationDrawable.selectDrawable(0);
  }

  /**
   * 获取view的location
   */
  public static int[] getLocation(View view) {
    int[] position = new int[2];
    view.getLocationOnScreen(position);
    return position;
  }
}
